<template>
  <div class="app-container">
    <!-- <el-row style="margin-top:20px;font-size:14px;color:#666">
      <el-col :span="2">客户：{{ order.real_name }}</el-col>
      <el-col :span="3">医院：{{ order.sort_name }}</el-col>
      <el-col :span="4">项目：{{ project }}</el-col>
      <el-col :span="2">本金：{{ order.total_money }}</el-col>
      <el-col :span="2">总债权：{{ order.allmoney }}</el-col>
      <el-col :span="3">首打款：{{ order.first_pay }}</el-col>
      <el-col :span="2">回款周期：{{ order.hkzq }}</el-col>
      <el-col :span="3">结算账户：{{ order.settle_account }}</el-col>
      <el-col :span="2">已结算：{{ order.settle_money }}</el-col>

    </el-row> -->

    <el-table :key="tableKey" v-loading="listLoading" :data="list" :row-class-name="tableRowClassName" border fit highlight-current-row style="width: 100%;margin-top:20px">

      <el-table-column label="客户姓名" align="center" width="80px">
        <template slot-scope="{row}">
          <span>{{ row.real_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="付租期数" align="center" width="50px">
        <template slot-scope="{row}">
          <span>{{ row.num }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应付时间" align="center" width="100px">
        <template slot-scope="{row}">
          <span>{{ row.time_date }}</span>
        </template>
      </el-table-column>

      <!-- <el-table-column label="实际还款时间" align="center" width="110px">
          <template slot-scope="{row}">
            <span>{{ row.sh_time }}</span>
          </template>
        </el-table-column> -->
      <el-table-column label="付租状态" align="center" width="80px">
        <template slot-scope="{row}">
          <span>{{ row.status_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="付租金额" align="center" width="80px">
        <template slot-scope="{row}">
          <span>{{ row.total_money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="本金" align="center" width="80px">
        <template slot-scope="{row}">
          <span>{{ row.contract_infor.benjin }}</span>
        </template>
      </el-table-column>
      <el-table-column label="利息" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.contract_infor.money_rate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="加融" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.money_rate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="流量费" align="center" width="80px">
        <template slot-scope="{row}">
          <span>{{ row.contract_infor.flow }}</span>
        </template>
      </el-table-column>

      <el-table-column label="实际付租金额" align="center" width="110px">
        <template slot-scope="{row}">
          <div class="input-box">
            <el-input v-model="row.paid_money" size="small" prop="paid_money" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="实际付租日期" align="center" width="130px">
        <template slot-scope="{row}">
          <div class="block">
            <el-date-picker
              v-model="row.paid_date"
              style="width:140px"
              format="yyyy-MM-dd"
              type="datetime"
              placeholder="选择日期"
            />
          </div>
          <!-- <span v-if="row.is_paid==1">{{ row.paid_date }}</span> -->
        </template>
      </el-table-column>
      <el-table-column label="付租方式" align="center" width="130px">
        <template slot-scope="{row}">
          <el-select v-model="row.payway" placeholder="请选择">
            <el-option
              v-for="item in payway"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="是否已付" align="center" width="100px">
        <template slot-scope="{row}">
          <el-select v-model="row.is_paid_bf" placeholder="请选择">
            <el-option
              v-for="item in paid_options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </el-table-column>
      <!-- <el-table-column label="本金回款" align="center" width="90px">
        <template slot-scope="{row}">
          <span>{{ row.benjin }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否结算" align="center" width="50px">
        <template slot-scope="{row}">
          <span>{{ row.settle_name }}</span>
        </template>
      </el-table-column>  -->

      <!-- <el-table-column label="结算金额" align="center" width="110px">
        <template slot-scope="{row}">
          <div class="input-box">
            <el-input v-model="row.settle_money" size="small" prop="settle_money"  />
          </div>
        </template>
      </el-table-column> -->

      <el-table-column label="逾期天数" align="center" width="60px">
        <template slot-scope="{row}">
          <span>{{ row.late_num }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" :disabled="row.is_paid==1?true:false" @click="onSubmit(row)">
            更新账单
          </el-button>
          <!-- <el-button v-if="row.can_bond==1&&row.is_paid==1" type="error" size="mini" @click="toBond(row)">
            转为保证金
          </el-button> -->
        </template>
      </el-table-column>

      <el-table-column v-for="(item,k) in share_holder" :key="k" :label="item.shareholdername" width="70" align="center">
        <el-table-column prop="province" label="本金" width="70" align="center">
          <template slot-scope="{row,$index}">
            <span v-if="row.is_paid==1 && row.is_settle==1">{{ item.benjin_percentage }}</span>
            <span v-else />
          </template>
        </el-table-column>
        <el-table-column prop="province" label="分利" width="70" align="center">
          <template slot-scope="{row,$index}">
            <span v-if="row.is_paid==1 && row.is_settle==1">{{ item.all_benefits }}</span>
            <span v-else />
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="代转状态" align="center" width="110px">
        <template slot-scope="{row}">
          <el-select v-if="row.payway==8" v-model="row.get_status" placeholder="请选择">
            <el-option
              v-for="item in get_status"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </el-table-column>

      <el-table-column label="代转日期" align="center" width="130px">
        <template slot-scope="{row}">
          <div v-if="row.payway==8" class="block">
            <el-date-picker
              v-model="row.get_date"
              style="width:140px"
              format="yyyy-MM-dd"
              type="datetime"
              placeholder="选择日期"
            />
          </div>
          <!-- <span v-if="row.is_paid==1">{{ row.paid_date }}</span> -->
        </template>
      </el-table-column>

      <el-table-column label="代转操作" align="center" width="100" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button v-if="row.payway==8" type="primary" size="mini" @click="editDZstatus(row)">
            保存代转状态
          </el-button>
        </template>
      </el-table-column>

    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="query.page" :limit.sync="query.limit" @pagination="getList" />
  </div>
</template>

<script>
import {
  paymentList, paymentEdit, toBond, getPayWay, editDZstatus
} from '@/api/finance'
import { formatDate } from '@/utils/formatDate.js'
import { merchantList } from '@/api/merchant'
import { fetchList } from '@/api/sale'
import waves from '@/directive/waves' // waves directive
import {
  parseTime
} from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'ComplexTable',
  components: {
    Pagination
  },
  directives: {
    waves
  },
  filters: {},
  data() {
    return {
      tableKey: 0,
      tableRecord: 1,
      list: null,
      payway: [],
      order: [],
      get_status: [
        {
          value: 1,
          label: '是'
        }, {
          value: 0,
          label: '否'
        }
      ],
      options: [{
        value: 1,
        label: '是'
      }, {
        value: 0,
        label: '否'
      }],
      paid_options: [{
        value: 1,
        label: '是'
      }, {
        value: 0,
        label: '否'
      }, {
        value: 2,
        label: '部分'
      }],
      project: '',
      record_list: null,
      listRecordLoading: true,
      recordTotal: 0,
      total: 0,
      listLoading: true,
      query: {
        order_id: '',
        page: 1,
        limit: 10
      },
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date())
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24)
            picker.$emit('pick', date)
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', date)
          }
        }]
      },
      value1: '',
      value2: '',
      value3: ''
    }
  },
  created() {
    this.getList()
    this.getPayWay()
  },
  methods: {
    getList() {
      this.query.order_id = this.$route.params.order_id
      paymentList(this.query).then(response => {
        this.list = response.data.list.data
        this.total = response.data.list.total
        this.order = response.data.order
        this.project = response.data.project
        this.share_holder = response.data.share_holder
        this.listLoading = false
      })
    },
    getPayWay() {
      getPayWay().then(res => {
        this.payway = res.data
      })
    },
    tableRowClassName({ row, rowIndex }) {
      if (row.is_paid == 1) {
        return 'success-row'
      } else if (row.is_paid == 2) {
        return 'warning-row'
      }

      return ''
    },
    RowClassName({ row, rowIndex }) {
      console.log(123123123)
      if (row.payway == 8) {
        return 'show_lab'
      } else {
        return 'hide_lab'
      }
      return ''
    },
    onSubmit(row) {
      const pam = {}
      pam.money_rate = row.money_rate
      pam.is_paid_bf = row.is_paid_bf
      pam.late_fee = row.late_fee
      pam.paid_money = row.paid_money// 实际还款金额
      pam.settle_money = row.settle_money// 结算金额
      pam.pay_id = row.pay_id
      pam.payway = row.payway
      if (row.settle_day != null) {
        pam.settle_date = row.settle_day// 结算日期
      } else {
        pam.settle_date = this.value1// 结算日期
      }
      // pam.settle_date = this.value1// 结算日期
      pam.paid_date = row.paid_date// 实际付款日期
      pam.is_settle = row.is_settle// 实际付款日期
      // console.log(this.value1);
      // console.log(pam);return;
      paymentEdit(pam).then(resp => {
        this.$notify({
          title: '提示',
          message: '修改成功',
          type: 'success',
          duration: 2000,
          onClose: this.getList()
        })
      })
    },
    editDZstatus(row) {
      const pam = {}

      pam.pay_id = row.pay_id
      pam.get_status = row.get_status
      pam.get_date = row.get_date

      editDZstatus(pam).then(resp => {
        this.$notify({
          title: '提示',
          message: '修改成功',
          type: 'success',
          duration: 2000,
          onClose: this.getList()
        })
      })
    },
    toBond(row) {
      this.$confirm(
        '确定将该条还款记录设置为保证金?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        const pam = {}
        pam.pay_id = row.pay_id
        pam.order_id = row.order_id
        toBond(pam).then(resp => {
          this.$notify({
            title: '提示',
            message: '修改成功',
            type: 'success',
            duration: 2000,
            onClose: this.getList()
          })
        })
      })
    }

  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    white-space: nowrap;
  }
  .show_lab{
    display: show;
  }
  .hide_lab{
    display: none;
  }
  select{
    width:100px;
  }
  .el-table .warning-row {
      background: orangered;
    }

  .el-table .success-row {
    background: #98dd72;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .tb-edit .input-box {
    display: none
  }
  .tb-edit .current-cell .input-box {
    display: block;
    margin-left: -15px;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
    float: left;
  }
</style>
